"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const cheerio_1 = __importDefault(require("cheerio"));
const Article_1 = __importDefault(require("../model/Article"));
function getAllUrl(url) {
    return __awaiter(this, void 0, void 0, function* () {
        const { data: html } = yield axios_1.default.get(url);
        const $ = cheerio_1.default.load(html);
        const [...data] = $(".blog-post>div>div:nth-of-type(1)>a");
        const path = data.map((item) => item.attribs.href);
        const result = yield getData(path);
        yield Article_1.default.bulkCreate(result);
        console.log("ok");
    });
}
let i = 0;
let id = setInterval(() => {
    i++;
    getAllUrl(`https://www.ailoli.org/page/${i}/`);
    if (i >= 6)
        return clearInterval(id);
}, 3000);
getData(["https://www.ailoli.org/archives/123/"]);
function getData(url) {
    return __awaiter(this, void 0, void 0, function* () {
        const result = url.map((item) => __awaiter(this, void 0, void 0, function* () {
            var _a;
            const { data: html } = yield axios_1.default.get(item);
            const $ = cheerio_1.default.load(html);
            const content = (_a = $("#postpage").html()) === null || _a === void 0 ? void 0 : _a.replace(/[\s]/g, "");
            let contentlenth = content === null || content === void 0 ? void 0 : content.length;
            const titles = $("#postpage").text().replace(/[ \n]/g, "").slice(0, 100) + "...";
            const title = $("#small_widgets>h1").text().trim();
            const author = $("#small_widgets>ul>li:nth-of-type(1)>a").text().trim();
            let time = $("#small_widgets>ul>li:nth-of-type(2)>time")
                .text()
                .trim();
            let str1 = time.replace(/[ ]/g, "");
            let str2 = str1.slice(str1.indexOf(",") + 1);
            const createdTime = parseInt(str2 + str1.slice(str1.indexOf(",") - 1, str1.indexOf(","))) - 1;
            const browesNum = parseInt($("#small_widgets>ul>li:nth-of-type(3)>span:nth-of-type(2)").text().trim());
            let comment = parseInt($("#small_widgets>ul>li:nth-of-type(4)>a").text().trim());
            comment = comment ? comment : 0;
            const fontlenth = parseInt($("#small_widgets>ul>li:nth-of-type(5)>span:nth-of-type(2)").text().trim());
            return {
                content,
                title,
                author,
                createdTime,
                browesNum,
                comment,
                fontlenth,
                titles,
                contentlenth,
            };
        }));
        const data = yield Promise.all(result);
        return data;
    });
}
